<template>
  <el-dialog :title="AddForm.id?'编辑权限':'添加权限'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="AddForm" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="AddForm.title" />
      </el-form-item>
      <el-form-item label="标识" prop="code">
        <el-input v-model="AddForm.code" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="AddForm.description" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" size="small" @click="submit">确认</el-button>
      <el-button size="small" @click="closeFn">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermission, editPermission } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      AddForm: {
        title: '', // 标题
        code: '', // 标识
        description: '', // 描述
        pid: '0', // 父权限 id, 没有的话就是 0
        type: '1'// 层级类型 1 或者 2，1表示一级页面权限
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 提交更新
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.AddForm.id) {
            await editPermission(this.AddForm)
          } else {
            await addPermission(this.AddForm)
          }
          this.closeFn()
          this.$emit('updateEvent')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog')
      this.AddForm = {
        title: '',
        code: '',
        description: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
